﻿using UnityEngine;

namespace Assets.Scripts.DL.Enemy.CommonEnemy.States {
	public class Move : CommonState<Controller> {
		Timer _timer;
		float _moveTime;//朝某个方向位移的最大时间
		bool _moveOver;

		public Move(Controller controller, StateEnum stateName) : base(controller, stateName) {
			AddConditionToSelf(() => true);


			AddNewTransition(StateEnum.Idle, () => _moveOver);
			_timer = new Timer();
		}

		public override void Enter() {
			if (_controller.IsFindPlayer() && !_controller.CanAtk()) {
				_controller.ChangeFaceDir();
			}
			_controller.PlayAnim(StateEnum.Move);
			var dir = _controller.FaceDir;
			_controller.Velocity = new UnityEngine.Vector2(dir * _controller.Config.MoveSpeed, 0);
			//Debug.Log(_controller.Velocity);
			_timer.Reset();
			var range = _controller.Config.MoveTimeRange;
			_moveTime = Random.Range(range.x, range.y);
			_moveOver = false;
		}



		public override void Update() {
			if (_controller.IsTouchingWall() || !_controller.IsOnGround()) {
				_moveOver = true;
			}

			if (!_controller.IsFindPlayer()) {
				if (_timer.Elapsed > _moveTime) {
					_moveOver = true;
				}
			}
		}
		public override void Exit() {
			//Debug.Log("run out");
		}
	}
}
